package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.model.TaskReceiver;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.comp.CommonDialog;
import ge.telasi.tasks.ui.log.LoggerUtils;
import java.awt.Component;
import java.util.List;

/**
 * @author dimitri
 */
public class TaskReceiverEditDialog extends CommonDialog {

    public TaskReceiverEditDialog(Component parent) {
        super(parent, true);
        initComponents();
        getRootPane().setDefaultButton(btnChange);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        receiverPanel1 = new ge.telasi.tasks.ui.task.TaskReceiverEditPanel();
        logger = new ge.telasi.tasks.ui.log.InlineLogger();
        btnCancel = new javax.swing.JButton();
        btnChange = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("შემსრულებელთა რედაქტირება");

        btnCancel.setText("გაუქმება");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnChange.setText("შეცვლა");
        btnChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnCancel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 211, Short.MAX_VALUE)
                .addComponent(btnChange)
                .addContainerGap())
            .addComponent(receiverPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(logger, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(receiverPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logger, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnChange))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeActionPerformed
        if (task != null && Platform.getDefault().validateConnection()) {
            List<TaskReceiver> receivers = getReceivers();
            Platform pl = Platform.getDefault();
            try {
                task = pl.getFacade().changeReceivers(pl.getCredentials(), task, receivers, copy);
                approved = true;
                dispose();
            } catch (Exception ex) {
                LoggerUtils.manageException(this, logger, ex);
            }
        }
}//GEN-LAST:event_btnChangeActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        approved = false;
        dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnChange;
    private ge.telasi.tasks.ui.log.InlineLogger logger;
    private ge.telasi.tasks.ui.task.TaskReceiverEditPanel receiverPanel1;
    // End of variables declaration//GEN-END:variables

    private Task task;
    private boolean copy;
    private boolean approved;

    public Task getTask() {
        return task;
    }

    public void setTask(Task task, boolean copy) {
        this.task = task;
        this.copy = copy;
        receiverPanel1.setReceivers(copy ? task.getCopies() : task.getAssignees());
        setTitle(copy ? "ასლი გაეგზავნოს" : "შესრულება დაევალოს");
    }

    public boolean isCopy() {
        return copy;
    }

    public List<TaskReceiver> getReceivers() {
        return receiverPanel1.getReceivers();
    }

    public boolean isApproved() {
        return approved;
    }

}
